﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.DataStore;
using NTTDATA.BEENChina.ResultCertification.model;
using NTTDATA.BEENChina.ResultCertification.Main.ServiceResultCertification;

namespace NTTDATA.BEENChina.ResultCertification.UI
{
    public partial class FormVerfityList : Form
    {
        public FormVerfityList()
        {
            InitializeComponent();
            InitCategoryType();
        }

        private void InitCategoryType()
        {
            this.comboBoxCategoryType.DataSource =  Enum.GetValues(typeof(EnumCategoryType));
        }

        private void Search()
        {          
            //DataStore.IVerfity verfityAccess  = new VerfityAccess();
            //List<VerfityItem> verfityItems = verfityAccess.GetVerfityItems(GetSearcheCategoryType());

            using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
            {
                List<VerfityItem> verfityItems = client.GetVerfityItems(GetSearcheCategoryType());

                if (verfityItems != null) 
                {
                    dataGridViewVerfityList.DataSource = verfityItems.OrderBy(t=>t.VerfityItemClassName).ToList();
                }
            }
        }

        private EnumCategoryType GetSearcheCategoryType()
        {
            EnumCategoryType categoryType;

            Enum.TryParse(comboBoxCategoryType.SelectedValue.ToString(), out categoryType);

            return categoryType;
        }

        private void Modify()
        {
          var selectRows = this.dataGridViewVerfityList.SelectedRows;

          if (selectRows.Count > 0)
          {
              string verfityItemId = selectRows[0].Cells["VerfityItemId"].Value.ToString();

              if (!String.IsNullOrEmpty(verfityItemId))
              {
                  FormVerfityModify verfityModify = new FormVerfityModify(GetSearcheCategoryType(),verfityItemId,EnumModify.Modify);

                  verfityModify.ShowDialog();
              }
          }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            Modify();
        }


        private void buttonAdd_Click(object sender, EventArgs e)
        {
            FormVerfityModify verfityModify = new FormVerfityModify(GetSearcheCategoryType(),EnumModify.Add);
            
            verfityModify.ShowDialog();
        }
    }
}
